from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from config import WRITE_DSN, READ_DSN

Base = declarative_base()
write_engine = create_engine(WRITE_DSN)
WriteSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=write_engine)
read_engine = create_engine(READ_DSN)
ReadSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=read_engine)


# Dependency
def get_write_db():
    db = WriteSessionLocal()
    try:
        yield db
    finally:
        db.close()


def get_read_db():
    db = ReadSessionLocal()
    try:
        yield db
    finally:
        db.close()
